package com.mofang.activity.recommend;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.Contacts.People.Phones;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mofang.R;
import com.mofang.activity.BaseTitleViewActiviey;
import com.mofang.activity.MainActivity;
import com.mofang.activity.MofangCommonUpdateView;
import com.mofang.controller.RecommendationController;
import com.mofang.model.RecommendationModel;
import com.pactera.framework.exception.IException;
import com.pactera.framework.model.MapEntity;
import com.pactera.framework.util.Loger;

public class MainRecommendActivity extends BaseTitleViewActiviey implements
		OnClickListener {

	private EditText recommended_edit;// 被推荐人
	private EditText recommendedPhone_edit;// 被推荐人电话
	private EditText intentionCity_edit;// 意向城市
	private TextView intentionDistrict_text;// 意向区域
	private EditText intentionBuilding_edit;// 意向楼盘
	private EditText intentionArea_edit;// 意向面积
	private EditText intentionTotalPrice_edit;// 意向总价
	private EditText specialRequirement_edit;// 特殊需求
	private LinearLayout main_recommend_ly;// 推荐按钮
	private LinearLayout intentionDistrict_ly;// 意向区域

	private ImageView recommend_recommendedPhone_img;
	private RecommendationController recommendationController;// 推荐
	private MainRecommendViewUpdate viewUpdateCallback;

	private static final int PICK_CONTACT = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void initView() {
		setContentView(R.layout.activity_main_recommend);
		setNavigationTitle("推荐买房赚佣金");
		findView();

	}

	private void findView() {
		recommended_edit = (EditText) findViewById(R.id.main_recommend_recommended_edit);

		recommendedPhone_edit = (EditText) findViewById(R.id.main_recommend_recommendedPhone_edit);

		intentionCity_edit = (EditText) findViewById(R.id.main_recommend_intentionCity_edit);

		intentionDistrict_text = (TextView) findViewById(R.id.main_recommend_intentionDistrict_text);

		intentionDistrict_ly = (LinearLayout) findViewById(R.id.main_recommend_intentionDistrict_ly);
		intentionDistrict_ly.setOnClickListener(this);

		intentionBuilding_edit = (EditText) findViewById(R.id.main_recommend_intentionBuilding_edit);

		intentionArea_edit = (EditText) findViewById(R.id.main_recommend_intentionArea_edit);

		intentionTotalPrice_edit = (EditText) findViewById(R.id.main_recommend_intentionTotalPrice_edit);

		specialRequirement_edit = (EditText) findViewById(R.id.main_recommend_specialRequirement_edit);

		main_recommend_ly = (LinearLayout) findViewById(R.id.main_recommend_ly);
		main_recommend_ly.setOnClickListener(this);

		recommend_recommendedPhone_img = (ImageView) findViewById(R.id.main_recommend_recommendedPhone_img);
		recommend_recommendedPhone_img.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent;
		switch (v.getId()) {
		case R.id.main_recommend_ly:
			String recommended = recommended_edit.getText().toString();
			String recommendedPhone = recommendedPhone_edit.getText()
					.toString();
			String intentionCity = intentionCity_edit.getText().toString();
			String intentionDistrict = intentionDistrict_text.getText()
					.toString();
			String intentionBuilding = intentionBuilding_edit.getText()
					.toString();
			String intentionArea = intentionArea_edit.getText().toString();
			String intentionTotalPrice = intentionTotalPrice_edit.getText()
					.toString();
			String specialRequirement = specialRequirement_edit.getText()
					.toString();

			// TODO 上传推荐
			if (recommendationController == null) {
				recommendationController = new RecommendationController();
			}
			if (viewUpdateCallback == null) {
				viewUpdateCallback = new MainRecommendViewUpdate(
						MainRecommendActivity.this);
			}
			MapEntity clerkEntity = new MapEntity();
			clerkEntity.setValue(RecommendationModel.Be_recommend_person_name,
					recommended);
			clerkEntity.setValue(RecommendationModel.Be_recommend_person_phone,
					recommendedPhone);
			recommendationController.addClerk(viewUpdateCallback, clerkEntity);
			intent = new Intent(getApplicationContext(),
					RecommendSuccessActivity.class);
			startActivity(intent);

			break;
		case R.id.main_recommend_intentionDistrict_ly:
			// TODO 跳转选择区域
			intent = new Intent(getApplicationContext(), MainActivity.class);
			startActivity(intent);
			break;

		case R.id.main_recommend_recommendedPhone_img:
			intent = new Intent(Intent.ACTION_PICK);
			intent.setType(ContactsContract.Contacts.CONTENT_TYPE);// vnd.android.cursor.dir/contact
			startActivityForResult(intent, PICK_CONTACT);
			break;
		}
	}

	@Override
	public void onActivityResult(int reqCode, int resultCode, Intent data) {
		super.onActivityResult(reqCode, resultCode, data);

		switch (reqCode) {
		case (PICK_CONTACT):
			if (resultCode == Activity.RESULT_OK) {
				Uri contactData = data.getData();
				Cursor c = managedQuery(contactData, null, null, null, null);
				if (c.moveToFirst()) {
					String name = c
							.getString(c
									.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
					recommended_edit.setText(name);
					int idColumn = c
							.getColumnIndex(ContactsContract.Contacts._ID);
					String contactId = c.getString(idColumn);
					int phoneCount = c
							.getInt(c
									.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
					String phoneNumber = null;
					if (phoneCount >= 1) {
						Cursor phones = getContentResolver()
								.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
										null,
										ContactsContract.CommonDataKinds.Phone.CONTACT_ID
												+ " = " + contactId, null, null);
						while (phones.moveToNext()) {
							phoneNumber = phones
									.getString(phones
											.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
							System.err.println(phoneNumber);
						}
						phones.close();
					}
					recommendedPhone_edit.setText(phoneNumber);
				}
			}
			break;
		}
	}

	class MainRecommendViewUpdate extends MofangCommonUpdateView<String> {

		public MainRecommendViewUpdate(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onPostExecute(String arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void handleException(IException ex) {
			// TODO Auto-generated method stub
			super.handleException(ex);
		}
	}
}
